﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data.Linq;
using System.Text;

using SmartProject.BusinessObjects;

namespace SmartProject.AppSpace
{
    public partial class Management : UI.BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                this.SetPage("Settings", "You can manage the global settings of SmartProject here. This page also allows you to add products and categories.");
                BindProducts(new Product());
                BindCategories(new Category());

                Preference preference = new Preference();
                preference.GetValue("DailyWorkHours");
                if (String.IsNullOrEmpty(preference.Value))
                {
                    preference.Save("DailyWorkHours", "8");
                    this.TxtDailyWorkHours.Text = "8";
                }
                else
                {
                    this.TxtDailyWorkHours.Text = preference.Value;
                }

                Logging.SeverityLevel severityLevel = new SmartProject.Logging.SeverityLevel();
                severityLevel.Populate(this.DrpLoggingLevel);
                preference.GetValue("LoggingLevelId");
                string loggingLevelId = preference.Value;
                if (!String.IsNullOrEmpty(loggingLevelId))
                {
                    this.DrpLoggingLevel.SelectedValue = loggingLevelId; 
                }   
            }
        }

        #region Bindings
        private void BindCategories(Category category)
        {
            category.Populate(this.DrpWorkCategories);
            this.BtnSaveCategory.CommandArgument = String.Empty;
            this.TxtCategory.Text = String.Empty;

            if (this.DrpWorkCategories.Items.Count == 0)
            {
                this.BtnEditWorkCategory.Visible = false;
                this.BtnRemoveWorkCategory.Visible = false;
            }
            else
            {
                this.BtnEditWorkCategory.Visible = true;
                this.BtnRemoveWorkCategory.Visible = true;
            }
        }

        private void BindProducts(Product product)
        {
            product.Populate(this.DrpProducts);
            this.TxtProductDescription.Text = String.Empty;
            this.BtnSaveProduct.CommandArgument = String.Empty;

            if (this.DrpProducts.Items.Count == 0)
            {
                this.BtnEditProduct.Visible = false;
                this.BtnRemoveProduct.Visible = false;
                this.BtnDiscontinueProduct.Visible = false;
            }
            else
            {
                this.BtnEditProduct.Visible = true;
                this.BtnRemoveProduct.Visible = true;
                this.BtnDiscontinueProduct.Visible = true;
            }
        }
        #endregion

        protected void BtnRemoveProduct_Click(object sender, EventArgs e)
        {
            bool isFirstTimeManagementSetup = IsFirstTimeManagementSetup;
            Product product = new Product();
            product.SetAttribute("ProductId", Convert.ToInt32(this.DrpProducts.SelectedValue));
            product.Remove();

            if (isFirstTimeManagementSetup)
            {
                // Force this page to refresh so that the links in the
                // dashboard can be shown.
                Response.Redirect("~/AppSpace/Management.aspx", true);
            }
            else
            {
                if (this.DrpProducts.Items.Count == 1)
                {
                    Response.Redirect("~/AppSpace/Management.aspx", true);
                    return;
                }
                BindProducts(product);
            }
        }

        protected void BtnDiscontinueProduct_Click(object sender, EventArgs e)
        {
            bool isFirstTimeManagementSetup = IsFirstTimeManagementSetup;
            Product product = new Product();
            product.SetAttribute("ProductId", Convert.ToInt32(this.DrpProducts.SelectedValue));
            product.Discontinue();

            if (isFirstTimeManagementSetup)
            {
                // Force this page to refresh so that the links in the
                // dashboard can be shown.
                Response.Redirect("~/AppSpace/Management.aspx", true);
            }
            else
            {
                if (this.DrpProducts.Items.Count == 1)
                {
                    Response.Redirect("~/AppSpace/Management.aspx", true);
                    return;
                }
                BindProducts(product);
            }
        }

        protected void BtnRemoveWorkCategory_Click(object sender, EventArgs e)
        {
            bool isFirstTimeManagementSetup = IsFirstTimeManagementSetup;
            Category category = new Category();
            category.Remove(Convert.ToInt32(this.DrpWorkCategories.SelectedValue));

            if (isFirstTimeManagementSetup)
            {
                // Force this page to refresh so that the links in the
                // dashboard can be shown.
                Response.Redirect("~/AppSpace/Management.aspx", true);
            }
            else
            {
                if (this.DrpWorkCategories.Items.Count == 1)
                {
                    Response.Redirect("~/AppSpace/Management.aspx", true);
                    return;
                }
                BindCategories(category);
            }
        }

        protected void BtnSaveProduct_Click(object sender, EventArgs e)
        {
            this.LblProductError.Text = String.Empty;
            Product product = new Product();
            try
            {
                product.SetAttribute("Description", this.TxtProductDescription.Text);
            }
            catch (Exceptions.RequiredDataParameter requiredDateParameter)
            {
                this.LblProductError.Text = requiredDateParameter.Message;
                this.PopProduct.Show();
                return;
            }

            bool isFirstTimeManagementSetup = IsFirstTimeManagementSetup;
            if (String.IsNullOrEmpty(this.BtnSaveProduct.CommandArgument))
            {
                product.SetAttribute("OwnerId", base.UserId);
                product.Save();
            }
            else
            {
                product.Update();
            }

            this.TxtProductDescription.Text = String.Empty;
            if (isFirstTimeManagementSetup)
            {
                // Force this page to refresh so that the links in the
                // dashboard can be shown.
                Response.Redirect("~/AppSpace/Management.aspx", true);
            }
            else
            {
                BindProducts(product);
            }
        }

        protected void BtnSaveCategory_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(this.TxtCategory.Text))
            {
                this.LblProductError.Text = "Category description is required.";
                this.PopCategory.Show();
                return;
            }

            Category category = new Category();
            bool isFirstTimeManagementSetup = IsFirstTimeManagementSetup;
            if (String.IsNullOrEmpty(this.BtnSaveCategory.CommandArgument))
            {

                category.Add(this.TxtCategory.Text);
            }
            else
            {
                category.Update(Convert.ToInt32(this.BtnSaveCategory.CommandArgument),
                    this.TxtCategory.Text);
            }

            if (isFirstTimeManagementSetup)
            {
                // Force this page to be refresh so that the links in the
                // dashboard can be shown.
                Response.Redirect("~/AppSpace/Management.aspx", true);
            }
            else
            {
                BindCategories(category);
            }
        }

        protected void BtnEditProduct_Click(object sender, EventArgs e)
        {
            this.LblProductDesc.Text = "Update Product";
            this.TxtProductDescription.Text = this.DrpProducts.SelectedItem.Text;
            this.BtnSaveProduct.CommandArgument = this.DrpProducts.SelectedValue;
            this.PopProduct.Show();
        }

        protected void BtnEditWorkCategory_Click(object sender, EventArgs e)
        {
            this.LblCategory.Text = "Update Category";
            this.TxtCategory.Text = this.DrpWorkCategories.SelectedItem.Text;
            this.BtnSaveCategory.CommandArgument = this.DrpWorkCategories.SelectedValue;
            this.PopCategory.Show();
        }

        protected void BtnSave_Click(object sender, EventArgs e)
        {
            this.ClearErrorOrWarning();
            Preference preference = new Preference();
            if (!String.IsNullOrEmpty(this.TxtDailyWorkHours.Text))
            {
                preference.Save("DailyWorkHours", this.TxtDailyWorkHours.Text);
            }
            else
            {
                this.DisplayErrorOrWarning("Daily Work Hours cannot be empty!");
            }
            preference.Save("LoggingLevelId", this.DrpLoggingLevel.SelectedValue);
        }

        public override void RefreshPage(object obj)
        {
            // Do nothing.
        }

        public override AjaxControlToolkit.ModalPopupExtender Popup(string name)
        {
            return null;
        }
    }
}
